package com.kosugi.flickingcoin;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.math.Vector3;


public class Image {
	
	public Vector3 pos;
	public float rotation;
	
    private Texture                         texture;
    private Mesh                            mesh;
	public Image(float width, float height, String fileName) {
        pos = new Vector3(0, 0, 0);
		mesh = new Mesh(true, 4, 6, 
        		new VertexAttribute(VertexAttributes.Usage.Position, 3,"attr_Position"),
                new VertexAttribute(Usage.TextureCoordinates, 2, "attr_texCoords")
        		);
        
        texture = new Texture(Gdx.files.internal(fileName));

        float halfWidth = width / 2.f;
        float halfHeight = height / 2.f;
        
        mesh.setVertices(new float[] { 
                         -halfWidth, -halfHeight, 0, 0, 1,
                         halfWidth, -halfHeight, 0, 1, 1,
                         halfWidth,  halfHeight, 0, 1, 0,
                         -halfWidth,  halfHeight, 0, 0, 0
        });
        mesh.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });
	}
	
	public void moveTo(float x, float y, float z) {
		pos.set(x, y, z);
	}
	
	public void moveTo(Vector3 p) {
		pos.set(p);
	}
	
	public void render(GL10 gl) {
		
		gl.glPushMatrix();
		
		gl.glTranslatef(pos.x, pos.y, pos.z);
	
		
		gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
		gl.glEnable(GL10.GL_BLEND);
		
        gl.glActiveTexture(GL10.GL_TEXTURE0);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        texture.bind();
        
        mesh.render(GL10.GL_TRIANGLES);
        
        gl.glPopMatrix();
	}
	
}
